
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>菜单管理</title>
    #include("../../common/res.html")
</head> 
<body class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
      <table  id="dg" style="width:100%;height:100%;"></table>
      <div id="tb" style="padding:0 30px;">
      	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" id="btn_enable">启用</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-undo" id="btn_disable">禁用</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" id="btn_add">添加</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" id="btn_edit">编辑</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" id="btn_remove">删除</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" id="bnt-grant">分配资源</a>
      </div>
    </div>
	<div id="wu-dialog"></div>
    <script type="text/javascript">
        $(function(){
            $('#dg').datagrid({    
                url:'#(ctx)/admin/role/getData', 
                idField:'id',
                method:'get',
                toolbar:"#tb",
                fitColumns:false,
                rownumbers:true,
                singleSelect:true,
                pagination:true,
                striped:true,
                checkOnSelect:false,
                selectOnCheck:false,
                fit:true,
                pageSize:20,
                sortName:'id',
                columns:[[    
					{field:'ck',checkbox:true},
                    {title:'角色ID',field:'id',width:100},    
                    {title:'角色名称',field:'name',width:100},
                    {title:'角色描述',field:'des',width:200},
                    {title:'是否启用',field:'status',width:80,formatter:function(val){if(val==1){return '启用';}else{return '禁用'}}},
                ]]  
            });
            $("#btn_search").click(function(){
                $('#dg').treegrid('load',{
                    name: $("#searchmenuName").val()
                });
            });
        });
        
		$(function(){
			$("#btn_add").click(function(){
				$('#wu-dialog').dialog({
					closed: false,
					modal:true,
                    title: "添加角色",
                    width: 800,    
                    height: 500,
                    iconCls:'icon-save',
                    href: '#(ctx)/admin/role/edit',  
					buttons: [{
						text: '确定',
						iconCls: 'icon-ok',
						handler: function(){
							$('#EditForm').submit();
						}
					}, {
						text: '取消',
						iconCls: 'icon-cancel',
						handler: function () {
							$('#wu-dialog').dialog('close');                    
						}
                    }],
                    onLoad:function(){
                        
                        $('#EditForm').form({    
                            url:"#(ctx)/admin/role/saveRole",    
                            onSubmit: function(data){
                                
                            },    
                            success:function(data){    
                                data=JSON.parse(data);
                                _toast(data.msg);
                                if(data.code==0){
                                	$('#dg').datagrid("reload");
                                }
                                $('#wu-dialog').dialog('close');
                            }    
                        });
                    }
				});
			});
			
            $("#btn_edit").click(function(){
                var row=$('#dg').datagrid("getSelected");
                if(!row){
                    _toast("请选中一行");
                    return;
                }
                $('#wu-dialog').dialog({
					closed: false,
					modal:true,
                    title: "编辑栏目",
                    width: 800,    
                    height: 500,
                    iconCls:'icon-save',
                    href: '#(ctx)/admin/role/edit',  
					buttons: [{
						text: '确定',
						iconCls: 'icon-ok',
						handler: function(){
							$('#EditForm').submit();
						}
					}, {
						text: '取消',
						iconCls: 'icon-cancel',
						handler: function () {
							$('#wu-dialog').dialog('close');                    
						}
                    }],
                    onLoad:function(){
                    	$('#EditForm').form('load',row);
                        $('#EditForm').form({
                            url:"#(ctx)/admin/role/saveRole",    
                            onSubmit: function(data){
                                data.id=row.id;
                            },    
                            success:function(data){    
                                data=JSON.parse(data);
                                _toast(data.msg);
                                if(data.code==0){
                                	$('#dg').datagrid("reload");
                                }
                                $('#wu-dialog').dialog('close');   
                            }    
                        });
                    }
				});
            });

            $("#btn_remove").click(function(){
            	var rows=$('#dg').datagrid("getChecked");
                var ids=[];
                for(var i in rows){
                    ids.push(rows[i].id);
                }
                if(rows.length==0){
                    _toast("请勾选一行");
                    return;
                }
                $.messager.confirm('确认对话框', '是否删除勾选项？', function(r){
                    if (r){
                      $.post("#(ctx)/admin/role/del",{ids:ids.join(',')},function(data){
                    	  _toast(data.msg);
                          if(data.code==0){
                        	  reloadGrid();
                          }
                      },'json');
                    }
                });

            });
            //资源授权
            $("#bnt-grant").click(function(){
            	var rows=$('#dg').datagrid("getChecked");
                var ids=[];
                for(var i in rows){
                    ids.push(rows[i].id);
                }
                if(rows.length==0){
                    _toast("请勾选一行");
                    return;
                }
                if(rows.length>1){
                    _toast("最多只能选中一行");
                    return;
                }
                layer.open({
					title:'给角色【'+rows[0].name+'】分配资源',
				    type: 2,
				    area: ['380px', '530px'],
				    fix: false, //不固定
				    maxmin: true,
				    content: '#(ctx)/admin/role/getZtree?roleId='+ids[0]+'&type=1'
				});
            });
            
            $("#btn_enable").click(function(){
            	setVisible(1);
            })
            $("#btn_disable").click(function(){
            	setVisible(0);
            })
		});
		function reloadGrid(){
        	$('#dg').datagrid("reload");
        }
		function setVisible(visible){
			var rows=$('#dg').datagrid("getChecked");
            var ids=[];
            for(var i in rows){
                ids.push(rows[i].id);
            }
            if(rows.length==0){
                _toast("至少选中一行");
                return;
            }
            var idstr=ids.join(",");
            var data={ids:idstr,visible:visible}
            $.post("#(ctx)/admin/role/setVisible",data,function(data){
            	_toast(data.data);
            	if(data.code==0){
            		reloadGrid();
            	}
            },'json');

		}
    </script>
</body> 
</html>
